package com.mxchip.livestar.ui.network.wifi.view


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.deeruu.system_help.Ethrenet
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.mxchip.livestar.R
import kotlinx.android.synthetic.main.fragment_ethernet.*
import java.util.regex.Matcher
import java.util.regex.Pattern


class EthernetFragment : Fragment() {


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_ethernet, container, false)
    }

    private val TAG = "EthernetFragment"
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var ips = com.deeruu.system_help.Ethrenet.getIps(requireContext())
        Log.d(TAG, "onViewCreated: ips:$ips")
        setTextCheckNotNull(ips["ipAddress"],ip_address_tv)


        ip_address_tv.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                checkError(ip_address_tv, ip_address)
            } else {
                ip_address.error = null
            }
        }
        setTextCheckNotNull(ips["gateWay"],gateway_tv)

        gateway_tv.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                checkError(gateway_tv, gateway)
            } else {
                gateway.error = null
            }
        }
        setTextCheckNotNull(ips["maskAddress"],net_mask_tv)
        net_mask_tv.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                checkError(net_mask_tv, net_mask)
            } else {
                net_mask.error = null
            }
        }
        setTextCheckNotNull(ips["dns"],dns_1_tv)
        dns_1_tv.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                checkError(dns_1_tv, dns_1)
            } else {
                dns_1.error = null
            }
        }

        dns_2_tv.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                checkError(dns_2_tv, dns_2)
            } else {
                dns_2.error = null
            }
        }

        connect_btn.setOnClickListener {
            var ipAddress = checkError(ip_address_tv, ip_address)
            var gateway = checkError(gateway_tv, gateway)
            var netMask = checkError(net_mask_tv, net_mask)
            var dns1 = checkError(dns_1_tv, dns_1)
            var dns2 = checkError(dns_2_tv, dns_2)
            if (ipAddress
                    && gateway
                    && dns1
                    && dns2
                    && netMask) {
              Ethrenet.setStaticIP(context,
                        ip_address_tv.text.toString(),
                        gateway_tv.text.toString(),
                        net_mask_tv.text.toString(),
                        dns_1_tv.text.toString(),
                        dns_2_tv.text.toString()

                )
                Toast.makeText(requireContext(), "设置完成", Toast.LENGTH_SHORT).show()
                requireActivity().finish()
            }
        }

    }

    private fun setTextCheckNotNull(str: String?, textView: TextInputEditText?) {
        if (!str.isNullOrEmpty()) {
            textView?.setText(str)
        }

    }

    private fun checkError(ipAddressTv: TextInputEditText, inputLayout: TextInputLayout): Boolean {
        if (!isIP(ipAddressTv.text.toString())) {
            inputLayout.error = "格式错误"
            return false
        }
        return true
    }

    fun isIP(addr: String): Boolean {
        if (addr.length < 7 || addr.length > 15 || "" == addr) {
            return false
        }
        /**
         * 判断IP格式和范围
         */
        val rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"
        val pat: Pattern = Pattern.compile(rexp)
        val mat: Matcher = pat.matcher(addr)
        return mat.find()
    }

}
